package cn.edu.scau.cmi.ema.controller;

import java.util.Set;
import java.util.List;
import cn.edu.scau.cmi.ema.util.CmiHibernateProxyUtil;
import cn.edu.scau.cmi.ema.dao.*;
import cn.edu.scau.cmi.ema.domain.*;
import cn.edu.scau.cmi.ema.service.*;
import cn.edu.scau.cmi.ema.service.pagination.*;
import cn.edu.scau.cmi.ema.controller.base.CmiEmaController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.*;
import cn.edu.scau.cmi.ema.domain.base.CmiPagination;

@Controller("AdulterlinkController")
public class AdulterlinkController extends CmiEmaController {

	@RequestMapping("/listAdulterlinks")
	public ModelAndView listAdulterlinks(HttpServletRequest request, @ModelAttribute CmiPagination pagination) {
		ModelAndView mav = new ModelAndView();

		pagination = adulterlinkPaginationService.updatePagination(request, pagination);
		Set<Adulterlink> adulterlinks = adulterlinkPaginationService.getPaginationAdulterlinks(pagination);

		mav.addObject("adulterlinks", adulterlinks);
		mav.addObject("entityName", "adulterlink");
		mav.addObject("pagination", pagination);
		mav.setViewName("adulterlink/listAdulterlinks.jsp");
		return mav;
	}

	public String listAdulterlinks() {
		return "redirect:/listAdulterlinks";
	}

	@RequestMapping("/viewAdulterlink")
	public ModelAndView viewAdulterlink(@RequestParam Integer adulterlinkId) {
		ModelAndView mav = new ModelAndView();
		mav.addObject("adulterlink", adulterlinkDAO.findAdulterlinkByPrimaryKey(adulterlinkId));
		mav.setViewName("adulterlink/viewAdulterlink.jsp");
		return mav;
	}

	@RequestMapping(value = "/confirmDeleteAdulterlinks")
	public ModelAndView confirmDeleteAdulterlinks(
			@RequestParam(value = "deleteAdulterlinksIds", required = true) Set<Integer> deleteAdulterlinksIds) {
		ModelAndView mav = new ModelAndView();

		Set<Adulterlink> deleteAdulterlinks = adulterlinkDAO.findAdulterlinksByIds(deleteAdulterlinksIds);
		Set<Adulterlink> referencedAdulterlinks = adulterlinkService.loadReferenceAdulterlinks(deleteAdulterlinks);
		boolean hasReferenced;
		if (referencedAdulterlinks.size() == 0) {
			mav.addObject("deleteEntitys", deleteAdulterlinks);
			hasReferenced = false;
		} else {
			deleteAdulterlinks.removeAll(referencedAdulterlinks);
			mav.addObject("referencedEntitys", referencedAdulterlinks);
			mav.addObject("deleteEntitys", deleteAdulterlinks);
			hasReferenced = true;
		}
		mav.addObject("entityName", "Adulterlink");
		mav.addObject("hasReferenced", hasReferenced);
		mav.setViewName("forward:/listAdulterlinks");
		return mav;
	}

	@RequestMapping("/deleteAdulterlink")
	public String deleteAdulterlink(@RequestParam Integer adulterlinkId) {
		Adulterlink adulterlink = adulterlinkDAO.findAdulterlinkByPrimaryKey(adulterlinkId);
		adulterlinkService.deleteAdulterlink(adulterlink);
		return "forward:/listAdulterlinks";
	}

	@RequestMapping("/confirmDeleteAdulterlink")
	public ModelAndView confirmDeleteAdulterlink(@RequestParam Integer adulterlinkId) {

		ModelAndView mav = new ModelAndView();

		mav.addObject("adulterlink", adulterlinkDAO.findAdulterlinkByPrimaryKey(adulterlinkId));

		mav.setViewName("adulterlink/deleteAdulterlink.jsp");

		return mav;
	}

	@RequestMapping("/deleteAdulterlinks")
	public String deleteAdulterlinks(
			@RequestParam(value = "deleteAdulterlinksId", required = true) Set<Integer> deleteAdulterlinksId) {
		adulterlinkService.deleteAdulterlinks(deleteAdulterlinksId);
		return "forward:/listAdulterlinks";
	}

	@RequestMapping("/editAdulterlink")
	public ModelAndView editAdulterlink(@RequestParam Integer adulterlinkId) {

		ModelAndView mav = new ModelAndView();

		Adulterlink adulterlink = adulterlinkDAO.findAdulterlinkByPrimaryKey(adulterlinkId);
		// 将需要编辑的对象加到mav对象中
		mav.addObject("adulterlink", adulterlink);

		// 把所有的引用adulterlink对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(adulterlinkService.loadAdulterlinkObjectReferencedSet(adulterlink));

		// 将编辑页面adulterlink添加到mav中
		mav.setViewName("adulterlink/editAdulterlink.jsp");
		return mav;
	}

	@RequestMapping("/newAdulterlink")
	public ModelAndView newAdulterlink() {
		ModelAndView mav = new ModelAndView();
		Adulterlink adulterlink = new Adulterlink();

		mav.addObject("adulterlink", adulterlink);

		// 把所有的引用adulterlink对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(adulterlinkService.loadAdulterlinkObjectReferencedSet(adulterlink));

		mav.addObject("newFlag", true);
		mav.setViewName("adulterlink/editAdulterlink.jsp");

		return mav;
	}

	@RequestMapping("/saveAdulterlink")
	public ModelAndView saveAdulterlink(@ModelAttribute Adulterlink adulterlink) {

		ModelAndView mav = new ModelAndView();

		Adulterlink existingAdulterlink = CmiHibernateProxyUtil
				.deproxy(adulterlinkDAO.findAdulterlinkByPhysicalPrimaryKey(adulterlink.getName()));

		// 新建记录
		if (adulterlink.getId() == null) {
			if (adulterlink.equals(existingAdulterlink)) {

				mav.addAllObjects(adulterlinkService.loadAdulterlinkClassReferencedSet(adulterlink));
				// mav = editContinue(mav, adulterlink);
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("adulterlink/editAdulterlink.jsp");
			} else {
				adulterlinkService.saveAdulterlink(adulterlink);
				mav.setViewName("forward:/listAdulterlinks");
			}
		}
		// 编辑记录
		else {
			Adulterlink originAdulterlink = CmiHibernateProxyUtil
					.deproxy(adulterlinkDAO.findAdulterlinkByPrimaryKey(adulterlink.getId()));
			if (!adulterlink.equals(originAdulterlink) && adulterlink.equals(existingAdulterlink)) {
				mav.addAllObjects(adulterlinkService.loadAdulterlinkClassReferencedSet(adulterlink));
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("adulterlink/editAdulterlink.jsp");
			} else {
				adulterlinkService.saveAdulterlink(adulterlink);
				mav.setViewName("forward:/listAdulterlinks");
			}
		}
		return mav;
	}

	@RequestMapping("/adulterlinkController/binary.action")
	public ModelAndView streamBinary(@ModelAttribute HttpServletRequest request,
			@ModelAttribute HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("streamedBinaryContentView");
		return mav;

	}

}
